package com.ship.contract.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ship.contract.bean.ConResourceFullName;
import com.ship.contract.bean.ConResourceMemorandum;
import com.ship.contract.bean.ConResourceMemorandumPosition;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author tangshixuan
 * @date 2023-8-31
 */
public interface ConResourceMemorandumMapper extends BaseMapper<ConResourceMemorandum> {

    /**
     * 根据id查询所有信息
     */
    @Select("SELECT * FROM con_resource_memorandum WHERE id = #{id}")
    ConResourceMemorandum getById(@Param("id") Long id);

    /**
     * 获取所有合同编号
     * @return
     */
    @Select("SELECT contract_number FROM con_resource_memorandum")
    List<String> getAllContractNumbers();

    @Select("SELECT COUNT(sds.id) FROM sp_dispatch_scheme sds \n" +
            "LEFT JOIN sp_dispatch_scheme_detail sdsd on sdsd.scheme_id = sds.id \n" +
            "where sds.is_history = 0 AND sdsd.resource_contract = #{id} ")
    Integer getDispatchUseCountByContractId(@Param("id") Long id);

    @Update("UPDATE con_resource_memorandum_position SET con_id = #{conId} WHERE file = #{file}")
    int updateConId(@Param("file") String file,@Param("conId") Long conId);

    @Select("SELECT filed_name, x1, x2, y1, y2, page,filed_value FROM con_resource_memorandum_position WHERE con_id = #{conId}")
    List<ConResourceMemorandumPosition> getFieldLocation(@Param("conId") Long conId);

    @Select("select sbpm.time_zone from sp_boats_port_message sbpm where sbpm .enable = '1' and sbpm.port_name_en = #{portName} order by sbpm.create_time desc LIMIT 1")
    String queryTimeZoneByPortName(@Param("portName") String portName);

    @Select("select sbpm.time_zone from sp_boats_receiving_station sbrs left join sp_boats_port_message sbpm on sbrs.port = sbpm.port_name_en where sbrs.enable = '1' and sbrs.name = #{portName} order by sbpm.create_time desc LIMIT 1")
    String queryTimeZoneByTerminalName(@Param("portName") String portName);

    @Select("select con_id from con_resource_memorandum_resource where username= #{username}")
    List<String> getConIdByUser(@Param("username") String username);

}
